import store from '@/store'
import Wxml2Canvas from 'wxml2canvas'
//import poster from '@/components/common/tui-poster/tui-poster.js'
export default {
	computed: {
		appPrivacy: {
			get() {
				return store.getters.appPrivacy
			},
			set(val) {
				uni.setStorageSync('app:Privacy', val)
				store.state.appPrivacy = val
			}
		},
		appConfig: {
			get() {
				return store.getters.appConfig
			},
			set(val) {
				uni.setStorageSync('app:appConfig', val)
				store.state.appConfig = val
			}
		},
		appUserInfo: {
			get() {
				return store.getters.appUserInfo
			},
			set(val) {
				uni.setStorageSync('user:user_id', val.user_id)
				uni.setStorageSync('user:userInfo', val)
				store.state.userInfo = val
			}
		},
		appMemberInfo: {
			get() {
				return uni.getStorageSync('app:memberInfo')
				return store.getters.appMemberInfo
			},
			set(val) {
				uni.setStorageSync('app:memberInfo', val)
				store.state.memberData = val
			}
		},
		appIsLogin: {
			get() {
				return store.getters.appIsLogin
			},
			set(val) {
				uni.setStorageSync('app:isLogin', val)
				store.state.isLogin = val
			}
		},
	},
	date() {
		return {
			_view_width: '',
			_view_height: '',
			subscribe_template_id: null
		}
	},
	methods: {
		_openLoginShow() {
			uni.showModal({
				title: '会员提示',
				content: '此内容仅对登录会员开放，是否前往登录',
				cancelText: '取消',
				confirmText: '前往登录',
				success: function(res) {
					if (res.confirm) {
						this._openLogin()
					}
				}
			});
		},
		requestSubscribeMessage(ids) {
			let that = this;

			return new Promise((resolve, reject) => {
				if (!ids) {
					resolve()
				}
				console.log(ids)
				uni.requestSubscribeMessage({
					tmplIds: ids,
					success(res) {
						that.api.post('/v1/mini-program/subscribe-message', {
							data: res,
							ids: ids
						})
					},
					complete: response => {

						resolve(response);
					}
				})

			})
		},
		toMiniProgram(data) {
			wx.navigateToMiniProgram(data);
		},
		_openMap(latitude, longitude, name, address) {
			//this.showMap = true;
			uni.openLocation({
				latitude: parseFloat(latitude),
				longitude: parseFloat(longitude),
				name: name,
				address: address,
				success: function() {
					console.log('success');
				},
				fail: function() {
					console.log({
						latitude: latitude,
						longitude: longitude,
						name: name,
						address: address
					})
					uni.showToast({
						icon: "error",
						title: '打开地图失败',
					})
				},
			});
		},
		_toMobile(mobile) {
			wx.makePhoneCall({
				phoneNumber: mobile
			})
		},
		_openLogin() {
			this.pages.to('/pages/login/login')
		},
		_back() {
			this.pages.Back()
		},
		//动态获取画制作元素的宽高
		_viewCanvas(id) {
			uni.showLoading({
				title: '加载中'
			})
			const query = wx.createSelectorQuery().in(this)
			query.select(id).fields({
				size: true,
				scrollOffset: true
			}, data => {
				console.log(id, data)
				let width = data.width
				let height = data.height
				this._view_width = width
				this._view_height = height
				setTimeout(() => {
					this._vuewDraw()
				}, 1500)
			}).exec()
		},
		_vuewDraw() {
			uni.showLoading()
			let that = this
			//创建wxml2canvas对象
			console.log(this._view_width, this._view_height)
			let drawImage = new Wxml2Canvas({
				element: 'vuewCanvas', // canvas节点的id,
				obj: that, // 在组件中使用时，需要传入当前组件的this
				width: that._view_width * 2, // 宽高
				height: that._view_height * 4,
				background: '#fff', // 默认背景色
				progress(percent) { // 绘制进度
					console.log(percent)
					uni.showLoading({
						title: percent + '%'
					})
				},
				finish(url) {
					console.log('创建的图片', url)
					uni.hideLoading()
					that._showShareImage(url)
				},
				error(res) {
					console.log(res)
					uni.hideLoading()
					// 画失败的原因
				}
			}, that)
			let data = {
				//直接获取wxml数据
				list: [{
					type: 'wxml',
					class: '.teamTable-data',
					limit: '.teamTable-vuew',
					x: 0,
					y: 0
				}]
			}
			//传入数据，画制canvas图片
			drawImage.draw(data, this)
		},
		_previewImage(data, current) {
			console.log('_previewImage', data)
			uni.previewImage({
				urls: data,
				current: current,
			})
		},
		_downloadImage(images) {
			let that = this
			uni.downloadFile({
				url: images,

				success: (res) => {
					if (res.statusCode === 200) {
						that._showShareImage(res.tempFilePath)
					}
				}
			})
		},
		_showShareImage(path) {
			wx.showShareImageMenu({
				path: path
			})
		},
		_tost(title, icon) {
			uni.showToast({
				title: title,
				icon: icon ? icon : 'error'
			})
		},
		_to(url, data) {
			this.pages.to(url, data)
		},
		_getMemberBalance(balance_id) {
			return this.api.post('/balance/v1/balance/balance/get-member-balance', {
				balance_id: balance_id
			})
		},
		_copyText(data) {
			uni.setClipboardData({
				data: data,
				success: function() {
					uni.showToast({
						title: '复制成功',
						icon: 'success'
					})
				}
			})
		},
		// 将海报图片保存到本地
		_saveImage(file) {
			console.log(file)
			if (file.indexOf('https://') >= 0) {
				console.log(file)
				let that = this
				uni.downloadFile({
					url: file,
					success: (res) => {
						if (res.statusCode === 200) {
							//poster.saveImage(res.tempFilePath)

						}
					}
				})
			} else {
				console.log(file)
				//poster.saveImage(file)
			}
		}

	},
	onLoad(e) {

		if (Array.isArray(e)) {
			let param = e.join('&')
		}

		if (e.promoter_code && e.promoter_code != '') {
			//uni.setStorageSync('url:promoterCode', e.promoter_code)
		}
		//console.log(param)
	},
	onShow() {
		let pages = getCurrentPages()
		if (pages.length >= 1 && pages.length != 0) {
			let url = this.wechatShare.pageRouter = (pages[pages.length - 1]).$page.fullPath
			this.wechatShare.mpShare = {
				title: this.appConfig.wechatShareItitle, // 默认为小程序名称
				path: url, // 默认为当前页面路径
				imageUrl: this.appConfig.wechatShareImage // 默认为当前页面的截图
			}
			//console.log('wechatShare', this.wechatShare.mpShare)
			return
		}
	},
	onShareTimeline: function() {
		let Share = this.wechatShare.mpShare
		console.log('Share', Share)
		if (Share.path) {
			if (Share.path.indexOf('promoter_code=') < 0) {
				if (Share.path.indexOf('?') >= 0) {
					Share.path = Share.path + '&promoter_code=' + uni.getStorageSync('app:promoterCode')
				} else {
					Share.path = Share.path + '?promoter_code=' + uni.getStorageSync('app:promoterCode')
				}
			}
		}
		let url = Share.path.split('?')
		//Share.path = url[0]
		if (url[1]) {
			Share.query = url[1]
		}
		console.log('onShareTimeline', Share, url)
		return Share
	},
	onShareAppMessage() {
		let Share = this.wechatShare.mpShare
		console.log('Share', Share)
		if (Share.path) {
			if (Share.path.indexOf('promoter_code=') < 0) {
				if (Share.path.indexOf('?') >= 0) {
					Share.path = Share.path + '&promoter_code=' + uni.getStorageSync('app:promoterCode')
				} else {
					Share.path = Share.path + '?promoter_code=' + uni.getStorageSync('app:promoterCode')
				}
			}
		}
		console.log('onShareAppMessage', Share)
		return Share
	}
}